﻿using System.Collections.Generic;
using System.Linq;
using ARR.UI.Models;
using System.Windows.Input;
using System.Collections.ObjectModel;
using ARR.Entities;
using Telerik.Windows.Controls;
using ARR.Shared;
using ARR.UI.Commands;
using System;
using ARR.UI.Views;

namespace ARR.UI.ViewModels
{
    class GeneralCommentsViewModel : WorkspaceViewModel
    {
        private IGeneralCommentsModel model;
        private ICommand saveCommand;
        private ICommand cancelCommand;
        private ICommand deleteCommand;
        private ICommand editCommand;


        public GeneralCommentsViewModel(IGeneralCommentsModel model)
        {
            this.DisplayName = Labels.ComentariosDeReportes;
            this.model = model;
        }



        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(e => Save());
                }
                return saveCommand;
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                if (cancelCommand == null)
                {
                    cancelCommand = new RelayCommand(e => Cancel());
                }
                return cancelCommand;
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(e => Delete());
                }
                return deleteCommand;
            }
        }

        public ICommand EditCommand
        {
            get
            {
                if (editCommand == null)
                {
                    editCommand = new RelayCommand(e => Edit());
                }
                return editCommand;
            }
        }


        public ObservableCollection<GeneralComment> GeneralComments
        {
            get
            {
                return model.GeneralComments;
            }
        }
        
        public ObservableCollection<Category> Categories
        {
            get
            {
                return model.Categories;
            }
        }
        
        public GeneralComment SelectedComment { get; set; }        
        
        public ObservableCollection<RangeReview> RangeReviews
        {
            get
            {
                return model.RangeReviews;
            }
        }

        public Array Sections
        {
            get
            {
                return Enum.GetValues(typeof(Section));
            }
        }

        public Array ReportTypes
        {
            get
            {
                return Enum.GetValues(typeof(ARR.Entities.ReportType));
            }
        }



        private void Save()
        {
            if (AreValid())
            {
                model.Save();
                model.LoadGeneralComments();

                OnPropertyChanged<GeneralCommentsViewModel>(p => p.GeneralComments);
            }
        }

        private void Edit()
        {
            ARR.UI.Views.RichTextEditor richTextEditor = new ARR.UI.Views.RichTextEditor(SelectedComment);
            richTextEditor.ShowDialog();   
        }

        private void Cancel()
        {
            model.LoadGeneralComments();

            OnPropertyChanged<GeneralCommentsViewModel>(p => p.GeneralComments);
        }

        private void Delete()
        {

        }

        private bool AreValid()
        {
            foreach (GeneralComment comment in GeneralComments)
            {
                if (!comment.IsValid)
                    return false;

                if (comment.Pdf == null)
                {
                    Alert errorMsg = new Alert(string.Format("Para poder guardar deber editar y guardar el cuerpo del comentario {0} del reporte {1} pagina {2}", comment.PageTitle, comment.ReportType, comment.Page), "Falta agregar comentario", AlertType.Error);
                    errorMsg.ShowDialog();

                    return false;
                }
            }

            return true;
        }
    }
}
